layui.use(['table', 'layer', 'form', 'laydate', 'util', 'jquery', 'common'], function () {
    let table = layui.table;
    let layer = layui.layer;
    let form = layui.form;
    let laydate = layui.laydate;
    let util = layui.util;
    let $ = layui.$;
    let common = layui.common;

    /**
     * 渲染数据表格
     */
    table.render({
        elem: "#regionList"
        , id: 'regionList'
        , method: 'get'
        , url: '/table/region'
        , page: false
        , cols: [[
            {title: '区域名称', field: 'name', fixed: 'left'}
            , {title: '餐台座位数', field: 'capacity'}
            , {title: '最低消费', field: 'minimum_consumption'}
            , {title: '排序', field: 'sort_order'}
            , {title: '创建时间', field: 'create_time'}
            , {title: '修改时间', field: 'update_time'}
            , {title: '操作', toolbar: "#set"}
        ]]
        , event: true
    });
    table.on("tool(regionList)", function (obj) {
        let event_name = obj.event;
        let region_id = $(this).data('id');
        if (event_name === 'edit'){
            $.ajax({
                url: "/table/region_info",
                data: {
                    id: region_id
                },
                dataType: 'json',
                type: 'get',
                success: function (res) {
                    if (res.code === 0){
                        form.val("regionInfo", {
                            region_name: res.data['name'],
                            region_capacity: res.data['capacity'],
                            region_min: res.data['minimum_consumption'],
                            region_sort: res.data['sort_order'],
                            _token: res.data['token'],
                            id: res.data['id']
                        });
                        layer.open({
                            type: 1
                            , title: '修改餐台区域信息'
                            , shadeClose: true
                            , content: $("#editRegion")
                        })
                    }else {
                        common.tipMessage('暂无该餐台信息');
                    }
                }
            })
        }

        if (event_name === 'del'){
            $.ajax({
                url: '/table/del',
                data: {
                    id: region_id,
                },
                dataType: 'json',
                type: 'get',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.msg, 6, function () {
                            table.reload('regionList', {
                                method: "get"
                            });
                        });
                    }else {
                        common.tipMessage(res.msg);
                    }
                }
            })
        }
    });

    /**
     * 修改餐台区域信息
     */
    form.on("submit(editForm)", function (data) {
        $.ajax({
            url: '/table/region_edit',
            data: {
                name: data.field['region_name'],
                capacity: data.field['region_capacity'],
                minimum_consumption: data.field['region_min'],
                sort_order: data.field['region_sort'],
                id: data.field['id'],
                _token: data.field['_token']
            },
            type: 'post',
            dataType: "json",
            success: function (res) {
                if (res.code ==- 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('regionList', {
                            method: "get"
                        });
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            },
            error: function (res) {

            },
            before: function (res) {

            }
        });
        return false;
    });

    form.on('submit(addForm)', function (data) {
        $.ajax({
            url: '/table/region_edit',
            data: {
                name: data.field['region_name'],
                capacity: data.field['region_capacity'],
                minimum_consumption: data.field['region_min'],
                sort_order: data.field['region_sort'],
                id: data.field['id'],
                _token: data.field['_token']
            },
            type: 'post',
            dataType: "json",
            success: function (res) {
                if (res.code ==- 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('regionList', {
                            method: "get"
                        });
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            },
            error: function (res) {

            },
            before: function (res) {

            }
        });
        return false;
    });
    $("#addBtn").click(function () {
        layer.open({
            type: 1
            , title: '添加餐台区域信息'
            , shadeClose: true
            , content: $("#addRegion")
        });
    });
    form.on('submit(addForm)', function (data) {
        $.ajax({
            url: '/table/add',
            type: 'post',
            data: {
                _token: data.field['_token'],
                name: data.field['region_name'],
                capacity: data.field['region_capacity'],
                minimum_consumption: data.field['region_min'],
                sort_order: data.field['region_sort']
            },
            dataType: 'json',
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('regionList', {
                            method: "get"
                        });
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            },
            error: function (res) {

            },
            before: function () {

            }
        });
        return false;
    });
});
